#!/bin/sh

if [ $# -ge 1 ] ; then
	# use explicitely set driver
	driver=$1
	echo "Device driver forced: $driver"
else
	# detect the driver
	if [ ! $(lsmod | grep nvidia | wc -l) -eq 0 ] ; then
		driver="nvrm"
	elif [ ! $(lsmod | grep nouveau | wc -l) -eq 0 ] ; then
		driver="nouveau"
	elif [ ! $(zgrep NOUVEAU /proc/config.gz | grep y | wc -l) -eq 0 ] ; then
		driver="nouveau"
	elif [ ! $(lsmod | grep pscnv | wc -l) -eq 0 ] ; then
		driver="pscnv"
	else
		echo "Device driver not found"
		exit
	fi

	echo "Device driver detected: $driver"
fi

# create Driver.mk
cat > Driver.mk << EOF
#
# Copyright (C) Shinpei Kato
# All Rights Reserved
#
# This is automatically generated by autogen.sh script.
#

DRIVER_NAME=$driver
EOF

DRIVER=$(echo $driver | tr "a-z" "A-Z")

# create gdev_autogen.h
cat > gdev_autogen.h << EOF
/*
 * Copyright (C) Shinpei Kato
 * All Rights Reserved
 *
 * This is automatically generated by autogen.sh script.
 *
 */

#ifndef __GDEV_AUTOGEN_H__
#define __GDEV_AUTOGEN_H__

#define GDEV_DRIVER_$DRIVER

#endif
EOF
